//
//  TTWebViewController.m
//  TaoTuo
//
//  Created by admin on 2019/10/22.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTWebViewController.h"
#import <WebKit/WebKit.h>

@interface TTWebViewController ()//<WKUIDelegate, WKNavigationDelegate>

@property (nonatomic, strong) WKWebView *webView;
@property (nonatomic, copy) NSString *headerString;

@property (nonatomic, strong) CALayer *placeholderLayer;

@end

@implementation TTWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.title = @"详情";
    // 添加个header，要不字体太小
    _headerString = @"<head><meta name='viewport' content='width=device-width, initial-scale=1'><style>img{max-width:100% !important; height:auto!important;}</style></head>";
    
    [self getNotifyContentData];
    
    self.placeholderLayer.hidden = NO;
}

- (void)getNotifyContentData
{
    @weakify(self)
    [[LLNetworkManagerTool sharedManager] requestMethod:GET urlString:notifyOrHelpDetail_api parameters:@{@"param.id" : self.notifyID} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        @strongify(self)
//        self.title = responseObject[@"Title"];
        NSString *title = responseObject[@"Title"];
//        NSString *time = responseObject[@"CreateTime"];
//        if (time.length > 10) {
//            time = [time substringToIndex:10];
//        }
        self.placeholderLayer.hidden = YES;
        NSString *content = [NSString stringWithFormat:@"%@<h3 align='center'>%@</h3>%@", self.headerString, title, responseObject[@"Content"]];
        [self.webView loadHTMLString:content baseURL:nil];
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nullable error) {
        
    } showLoadingInView:self.view];
}




- (WKWebView *)webView
{
    if (!_webView) {
        
        _webView = [[WKWebView alloc] init];
//        _webView.UIDelegate = self;
//        _webView.navigationDelegate = self;
        [self.view addSubview:_webView];
        [_webView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.mas_topLayoutGuideBottom);
            make.bottom.equalTo(self.mas_bottomLayoutGuideBottom);
            make.left.right.equalTo(self.view);
        }];
    }
    return _webView;
}

- (CALayer *)placeholderLayer
{
    if (!_placeholderLayer) {
        _placeholderLayer = [CALayer layer];
        _placeholderLayer.frame = CGRectMake(0, 0, 200, 90);
        _placeholderLayer.position = CGPointMake(self.view.center.x, self.view.center.y - 100);
        _placeholderLayer.contents = (__bridge id)[UIImage imageNamed:@"tao_nodata"].CGImage;
        _placeholderLayer.contentsScale = [UIScreen mainScreen].scale;
        _placeholderLayer.contentsGravity = kCAGravityResizeAspect;
        [self.webView.layer addSublayer:_placeholderLayer];
    }
    return _placeholderLayer;
}


@end
